import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/http/Res.dart';
import 'package:love_newf/app/com/http/http_request.dart';
import 'package:love_newf/app/com/model/room_prop_model.dart';
import 'package:love_newf/app/com/model/wallet_balance_model.dart';
import 'package:love_newf/app/com/service/wallet_service.dart';
import 'package:love_newf/app/com/util/gift_utils.dart';
import 'package:love_newf/app/com/util/my_toast.dart';
import 'package:love_newf/app/page/live/live_logic.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

class SendGiftDialog {
  SendGiftLogic logic = Get.put(SendGiftLogic());
  String userId = "";
  String headImg = "";
  WalletBalanceModel walletBalanceModel = WalletBalanceModel();
  WalletService walletService = WalletService();

  show({String userId = "", String headImg = ""}) async {
    this.userId = userId;
    this.headImg = headImg;
    walletService.getBalance().then((info) {
      if (info != null) {
        walletBalanceModel = info;
      }
      logic.update();
    });
    logic.roomPropList = filter(await GiftUtils.roomPropList);
    toast("aaa");

    if (logic.roomPropList.isEmpty) return;
    Get.bottomSheet(
        isScrollControlled: true,
        GetBuilder<SendGiftLogic>(
            builder: (l) => LColumn.eazy([
                  LExpand(),
                  LColumn.eazy([receiveUserBox, LExpand(child: propListBox)])
                      .bgColor(Colors.black45)
                      .size(375.w, Get.height / 10 * 6)
                      .onTapL(() {}),
                ]).size(Get.width, Get.height).onTapL(() => Get.back())));
  }

  get receiveUserBox => LRow.eazy([
        LImage.easy(headImg, 34.w, 34.w),
        LExpand(),
        LImage.easy(Assets.liveMaiRose, 30.w, 30.w),
        LText.easy(
            walletBalanceModel.tokens.toString(), 16.w, fw5, Colors.white),
        8.lsw,
        LText.easy("充值", 15.w, null, Colors.white)
            .padding(left: 6.w, right: 6.w, top: 4.w, bottom: 4.w)
            .gradient(const [Color(0xFFFFB851), Color(0xFFED8506)],
                Alignment.bottomCenter)
      ]).paddingSymmetric(horizontal: 12.w, vertical: 10.w);

  get propListBox => ListView(padding: EdgeInsets.zero, children: [
        Wrap(children: [
          for (int i = 0; i < logic.roomPropList.length; i++) itemView(i)
        ])
      ]);

  List<RoomPropModel> filter(List<RoomPropModel> list) {
    return list.where((RoomPropModel item) {
      return item.sell == 1 && item.action == 0 && item.type == 1;
    }).toList();
  }

  double itemWidth = Get.width / 4;

  itemView(int position) {
    RoomPropModel item = logic.roomPropList[position];

    var view = LColumn.eazy([
      5.lsh,
      LStack([
        LImage.easy(item.iconUrl, itemWidth - 5.w, itemWidth - 5.w),
        if (logic.selectIndex == position)
          LContainer(
                  child: LText.easy("赠送", 15.w, null, Colors.white)
                      .size(double.infinity, 34.w)
                      .center()
                      .bgColor(AppColors.primaryColor)
                      .onTapL(() {
            logic.giving(userId).then((bool v) {
              if (v) Get.back();
            });
          }))
              .bgColor(logic.selectIndex == position
                  ? Colors.black54
                  : Colors.transparent)
              .paddingAll(10.w)
              .size(itemWidth, itemWidth)
              .center()
      ]),
      LText.easy(item.name, 15.w, null, Colors.white).paddingV(4.w),
      LRow.eazy([
        LImage.easy(Assets.liveMaiRose, 23.w, 23.w),
        LText.easy(item.rosePrice, 15.w, null, Colors.white)
      ]),
      5.lsh
    ]).size(itemWidth, null).onTapL(() {
      logic.selectIndex = position;
      logic.update();
    });
    if (position == logic.selectIndex) {
      view.radius(5.w).border(1.w, Colors.blue);
    }
    return view;
  }
}

class SendGiftLogic extends GetxController {
  var tabNames = ["经典", "贴年"];
  List<RoomPropModel> roomPropList = [];
  int selectIndex = -1;

  Future<bool> giving(userId) async {
    Res r = await HttpRequest().postRes("/api/consume/giveGift", {
      "anchorUserId": userId,
      "roomId": LiveLogic.logic?.roomInfo.id,
      "propId": roomPropList[selectIndex].id,
      "propAmount": 1
    });
    return r.isSuccess();
  }
}
